package com.ubtechinc.cruzr.launcher2.presenter;

import com.ubtechinc.cruzr.launcher2.adapter.NestAdapter;
import com.ubtechinc.cruzr.launcher2.callback.AdminiListener;
import com.ubtechinc.cruzr.launcher2.callback.AppListener;
import com.ubtechinc.cruzr.launcher2.callback.RxTimerListener;
import com.ubtechinc.cruzr.launcher2.model.AppManger;
import com.ubtechinc.cruzr.launcher2.model.entity.AppInfo;
import com.ubtechinc.cruzr.launcher2.model.helper.AdministratorObserve;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;
import com.ubtechinc.cruzr.launcher2.utlis.RxTimer;
import com.ubtechinc.cruzr.launcher2.utlis.UbtCantans;
import com.ubtechinc.cruzr.launcher2.view.AppView;
import com.ubtechinc.cruzr.launcher2.view.RootView;
import com.ubtechinc.cruzr.launcher2.widget.HorizontalPageLayoutManager;
import com.ubtechinc.cruzr.launcher2.widget.IclineRelaiveLayout;

import java.util.ArrayList;
import java.util.List;

public class AppPresenter implements AppListener, AdminiListener {
    private AppView mView;
    private RxTimer mTimer;
    private List<AppInfo> mAppList = new ArrayList<>();
    private AdministratorObserve mObserve;
    private long mStartTime;

    public AppPresenter(RootView view) {
        mView = (AppView) view;
        mObserve = new AdministratorObserve();
        mObserve.initSecurityApi(this);
        AppManger.get().attachListener(this, this);
        AppManger.get().loadApps(true,mObserve.isAdmini());
    }

    public void reLoadApp() {
        mStartTime = System.currentTimeMillis();
        AppManger.get().loadApps(false, mObserve.isAdmini());
    }

    @Override
    public void appUpdate(List<AppInfo> list) {
        LogUtils.e("load app time = " + (System.currentTimeMillis() - mStartTime));
        mAppList = list;
        mView.onAppChange();
    }

    @Override
    public void appInit(List<AppInfo> list) {
        LogUtils.e("load app time = " + (System.currentTimeMillis() - mStartTime));
        mAppList = list;
        mView.onAppInit();
    }

    public void updateBattery() {
        AppManger.get().updateBattery();
    }

    @Override
    public void onBatteryUpdate(String battery) {
        mView.onBatteryUpdate(battery);
    }

    @Override
    public void onThirdAppOpen() {
        mView.onThirdAppOpen();
    }

    @Override
    public void onEnterAdmini() {
        LogUtils.e("enter onEnterAdmini");
        mStartTime = System.currentTimeMillis();
        AppManger.get().loadApps(false, mObserve.isAdmini());
    }

    @Override
    public void onExitAdmini() {
        LogUtils.e("exit onExitAdmini");
        mStartTime = System.currentTimeMillis();
        AppManger.get().loadApps(false, mObserve.isAdmini());
    }

    public List<AppInfo> getData() {
        return mAppList;
    }

    public boolean isThirdStatus() {
        return AppManger.get().isThidStatus();
    }

    public void removeThirdAppStatus() {
        AppManger.get().removeThirdAppStatus();
    }

    public void enableDeleteState(boolean isdelete) {
        AppManger.get().enableDeleteModel(isdelete);
    }

    public boolean isAppDeleteState() {
        return AppManger.get().isAppDeleteState();
    }

    public boolean isAdmini() {
        return mObserve.isAdmini();
    }

    public void enterDeleteState() {
        AppManger.get().enableDeleteModel(true);
        AppManger.get().startDeleteAinimation();
        mView.onStartDeleteState();
    }

    public void stopItemAnimation() {
        AppManger.get().stopDeleteAinimation();
    }

    public void startItemAnimation(HorizontalPageLayoutManager gridLayoutManager, NestAdapter adapter, int page) {
        if (!isAppDeleteState()) {
            return;
        }
        int pagecount = UbtCantans.RECYCLE_COLUMNS * UbtCantans.RECYCLE_ROWS;
        int start = page * pagecount - 1;
        if (start < 0) {
            start = 0;
        }
        int end = start + pagecount;
        for (int i = start; i < end; i++) {
            if (gridLayoutManager != null & adapter != null) {
                IclineRelaiveLayout view = (IclineRelaiveLayout) gridLayoutManager.findViewByPosition(i);
                if (view != null) {
                    view.startDeleteAinimation();
                }
            }
        }
    }

    public void exitDeleteState(boolean isdeletestate) {
        if (isAppDeleteState() && isdeletestate) {
            final RxTimer timer = new RxTimer();
            timer.startTime(new RxTimerListener() {
                @Override
                public void onTimeNext() {
                    timer.closeTimer();
                    mView.onStopDeleteState();
                }
            }, 100, 100);
        }

    }

    public void startUpdateTime() {
        if (mTimer != null) {
            mTimer.closeTimer();
        }
        mTimer = new RxTimer();
        mTimer.startTime(new RxTimerListener() {
            @Override
            public void onTimeNext() {
                if(mView != null){
                    if (UbtCantans.CLOCK_VISIBLE) {
                        mView.onDateTimeChange();
                    }
                    mView.onBatteryUpdate(AppManger.get().getBatteryManager().getBatteryText());
                }
            }
        }, 2000, 1500);
    }

}
